package com.easy.web.controller;

import com.easy.web.model.SysConfig;
import com.easy.web.model.User;
import com.easy.web.service.SysConfigService;
import com.easy.web.service.UserService;
import com.easy.web.utils.EasyConstant;
import com.easy.web.utils.MD5Util;
import com.easy.web.utils.ViewUtils;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/23.
 */
@Controller
@RequestMapping("sys")
public class SysConfigController {

    private static Logger logger = LoggerFactory.getLogger(SysConfigController.class);
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private UserService userService;

    /**
     * 获取系统配置列表
     *
     * @return
     */
    @RequestMapping(value = "getSysConfigList", method = {RequestMethod.GET})
    public ModelAndView getSysConfigList() {
        Map<String, Object> map = new HashedMap();
        List<SysConfig> sysConfigList = sysConfigService.getSysConfigList();
        for (SysConfig sysConfig : sysConfigList) {
            map.put(sysConfig.getConfigName(), sysConfig.getConfigValue());
        }

        return new ModelAndView("", ViewUtils.mapSuccess(map));
    }

    /**
     * 通过webSocket发送系统配置
     */
    private void sendSystemConfig(){
        List<SysConfig> sysConfigList = sysConfigService.getSysConfigList();
        HashedMap map = new HashedMap();
        for (SysConfig sysConfig : sysConfigList) {
            map.put(sysConfig.getConfigName(), sysConfig.getConfigValue());
        }
//        webSocketService.sendSystemConfig(map);
    }

    /**
     * 更新系统配置
     * 基本配置
     */

    @RequestMapping(value = "updateSysConfig", method = {RequestMethod.POST})
    public ModelAndView updateSysConfig(@RequestParam(value = "baseValue", required = false) String baseValue) {

//        UserVo userVo = (UserVo) request.getSession().getAttribute("userVo");
//
//        if (userVo == null) {
//            return new ModelAndView("", ViewUtils.mapFail("登陆超时，请重新等录"));
//        }

        try {
            JSONObject baseJson = JSONObject.fromObject(baseValue);
            SysConfig sysConfig = new SysConfig();
            if (!baseJson.isEmpty()) {
                //在线会员基数
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_TALK_ONLINEMEMBERBASE)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_TALK_ONLINEMEMBERBASE);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_TALK_ONLINEMEMBERBASE));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //网站标题
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_TITLE)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_TITLE);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_TITLE));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //网站logo
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_LOGO)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_LOGO);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_LOGO));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //关键字
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_KEYWORDS)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_KEYWORDS);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_KEYWORDS));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //描述
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_DESCRIPTION)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_DESCRIPTION);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_DESCRIPTION));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //视频地址
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_VIDEO_URL)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_VIDEO_URL);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_VIDEO_URL));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //公告
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_NOTICE)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_NOTICE);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_NOTICE));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //助理qq
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_ASSISTANT_QQ)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_ASSISTANT_QQ);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_ASSISTANT_QQ));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //助理介绍
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_ASSISTANT_INTRODUCE)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_ASSISTANT_INTRODUCE);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_ASSISTANT_INTRODUCE));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //发言时间间隔
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_TALK_TIME)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_TALK_TIME);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_TALK_TIME));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //客服名称
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_NAME)) {
                    String useStr = baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_NAME);
                    if (useStr != null) {
                        String[] uses = useStr.split(",");
                        for (String s : uses) {
                            User user = userService.queryUserByUserName(s);
                            if (user == null) {
                                return new ModelAndView("", ViewUtils.mapFail("客服名字:" + s + "不存在，请重新填写"));
                            }
                        }
                    }
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_NAME);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_NAME));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //客服qq
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_QQ)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_QQ);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_QQ));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //客服介绍
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_INTRODUCE)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_INTRODUCE);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_CUSTOM_INTRODUCE));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //课程
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_COURSE)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_COURSE);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_COURSE));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //视频标题
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_VIDEO_TITLE)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_VIDEO_TITLE);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_VIDEO_TITLE));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //会员服务
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_VIP_SERVICE)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_VIP_SERVICE);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_VIP_SERVICE));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //发言审核开关
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_SAY_AUDIT)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_SAY_AUDIT);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_SAY_AUDIT));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
                //海报图
                if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_POSTER_IMG)) {
                    sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_POSTER_IMG);
                    sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_POSTER_IMG));
                    sysConfigService.updateBaseSysConfig(sysConfig);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("保存配置失败：" + e.getMessage());
            return new ModelAndView("", ViewUtils.mapFail("保存失败"));
        }
        this.sendSystemConfig();
        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }

    /**
     * 系统配置
     * 权限等级配置
     */
    @RequestMapping(value = "updateRoleSysConfig", method = {RequestMethod.POST})
    public ModelAndView updateRoleSysConfig(@RequestParam(value = "baseValue", required = false) String baseValue) {

//        UserVo userVo = (UserVo) request.getSession().getAttribute("userVo");
//
//        if (userVo == null) {
//            return new ModelAndView("", ViewUtils.mapFail("登陆超时，请重新等录"));
//        }
        JSONObject baseJson = JSONObject.fromObject(baseValue);
        SysConfig sysConfig = new SysConfig();
        if (!baseJson.isEmpty()) {
            //踢人权限
            if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_ROLE_KICK)) {
                sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_ROLE_KICK);
                sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_ROLE_KICK));
                sysConfigService.updateBaseSysConfig(sysConfig);

            }
            //视频标题权限
            if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_COURSE_TITLE_ROLE)) {
                sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_COURSE_TITLE_ROLE);
                sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_COURSE_TITLE_ROLE));
                sysConfigService.updateBaseSysConfig(sysConfig);

            }
            //弹幕权限
            if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_BARRAGE_ROLE)) {
                sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_BARRAGE_ROLE);
                sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_BARRAGE_ROLE));
                sysConfigService.updateBaseSysConfig(sysConfig);
            }
            //后台登陆权限
            if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_ADMIN_LOGIN_ROLE)) {
                sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_ADMIN_LOGIN_ROLE);
                sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_ADMIN_LOGIN_ROLE));
                sysConfigService.updateBaseSysConfig(sysConfig);
            }
            //高级日刊权限
            if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_ARTICLE_VIP_ROLE)) {
                sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_ARTICLE_VIP_ROLE);
                sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_ARTICLE_VIP_ROLE));
                sysConfigService.updateBaseSysConfig(sysConfig);
            }
            //普通日刊权限
            if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_ARTICLE_ORD_ROLE)) {
                sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_ARTICLE_ORD_ROLE);
                sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_ARTICLE_ORD_ROLE));
                sysConfigService.updateBaseSysConfig(sysConfig);
            }

            //麦下权限
            if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_SUGGEST_ORD_ROLE)) {
                sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_SUGGEST_ORD_ROLE);
                sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_SUGGEST_ORD_ROLE));
                sysConfigService.updateBaseSysConfig(sysConfig);
            }
            //麦上权限
            if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_SUGGEST_ONLINE_ROLE)) {
                sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_SUGGEST_ONLINE_ROLE);
                sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_SUGGEST_ONLINE_ROLE));
                sysConfigService.updateBaseSysConfig(sysConfig);
            }
            //前台操作权限
            if (baseJson.containsKey(EasyConstant.WEBSITE_SYS_CONFIG_OPERATE_FRONT_ROLE)) {
                sysConfig.setConfigName(EasyConstant.WEBSITE_SYS_CONFIG_OPERATE_FRONT_ROLE);
                sysConfig.setConfigValue(baseJson.optString(EasyConstant.WEBSITE_SYS_CONFIG_OPERATE_FRONT_ROLE));
                sysConfigService.updateBaseSysConfig(sysConfig);
            }
        }
        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }

    /**
     * 后台登陆
     */
    @RequestMapping(value = "login", method = {RequestMethod.GET})
    public ModelAndView login(@RequestParam(value = "userName", required = true) String userName,
                              @RequestParam(value = "password", required = true) String password, HttpServletRequest request) {
        User user = null;
        try {
            user = userService.queryUserByUserName(userName);
        } catch (Exception e) {
            return new ModelAndView("", ViewUtils.mapFail("用户登陆失败"));
        }

        if (user != null) {
            SysConfig sysConfig = sysConfigService.querySysConfigByConfigName(EasyConstant.WEBSITE_SYS_CONFIG_ADMIN_LOGIN_ROLE);
            if (sysConfig.getConfigValue().contains(user.getLevel())) {
                if (!user.getPassword().equals(MD5Util.md5(password))) {
                    return new ModelAndView("", ViewUtils.mapFail("用户密码错误，请重新输入"));
                }
            } else {
                return new ModelAndView("", ViewUtils.mapFail("该用户无登陆权限"));
            }
        } else {
            return new ModelAndView("", ViewUtils.mapFail("用户名不存在，请重新输入"));
        }
        request.getSession().setAttribute("useName", userName);
        return new ModelAndView("", ViewUtils.mapSuccess(user));
    }

}
